package com.enfi.api.modules.ext.mq.strategy;

import com.alibaba.fastjson.JSONObject;
import com.enfi.api.domain.res.ApiResult;
import com.enfi.api.modules.ext.mq.common.MessageTypeEnum;
import com.enfi.api.modules.ext.mq.common.MqConstants;
import com.enfi.api.modules.ext.mq.common.PushTypeEnum;
import com.enfi.api.modules.ext.mq.common.PushWayEnum;
import com.enfi.api.modules.ext.mq.factory.MessageFactory;
import com.enfi.api.modules.ext.websocket.service.WsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 意见反馈策略
 * @author xukk
 * @date 2020/3/10
 */
@Service
@Slf4j
public class DefaultStrategy implements BaseStrategy, InitializingBean {
    private WsService wsService;
    @Override
    public void handle(JSONObject jsonObject) {
        Integer pushType=jsonObject.getInteger(MqConstants.ST.PUSH_TYPE);
        String pushWay=jsonObject.getString(MqConstants.ST.PUSH_WAY);
        if(pushType==null){
            // 默认走websocket
            pushType= PushTypeEnum.WEBSOCKET.getCode();
            jsonObject.put(MqConstants.ST.PUSH_TYPE,pushType);
        }
        if(StringUtils.isEmpty(pushWay)){
            pushWay= PushWayEnum.PC.name();
            jsonObject.put(MqConstants.ST.PUSH_WAY,pushWay);
        }
        ApiResult apiResult=null;
        switch (pushType){
            case 0:
                // websocket
                apiResult=wsService.handleFromMq(jsonObject);
                break;
            default:
                apiResult=ApiResult.renderError("pushType缺失或者不正确");
        }
        log.warn("此次处理结果：{}",apiResult);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        MessageFactory.register(MessageTypeEnum.DEFAULT, this);
    }
    @Autowired
    public void setWsService(WsService wsService) {
        this.wsService = wsService;
    }
}
